package com.jia.blog.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jia.blog.common.lang.R;
import com.jia.blog.entity.Category;
import com.jia.blog.service.CategoryService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jiajia
 * @since 2020-12-02
 */
@RestController
@RequestMapping("/category")
@Api(tags = "分类管理")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    @GetMapping("/list")
    public R list() {
        List<Category> list = categoryService.list();
        return R.ok().data("list",list);
    }

    @GetMapping("/listOne")
    public R listOne() {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",0L);
        wrapper.eq("status",1);
        List<Category> list = categoryService.list(wrapper);
        return R.ok().data("list",list);
    }

    @PostMapping("/save")
    public R save(@RequestBody Category category) {
        categoryService.save(category);
        return R.ok();
    }

    @PutMapping("/update")
    public R update(@RequestBody Category category){
        categoryService.updateById(category);
        return R.ok();
    }

    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") Long id){
        categoryService.removeById(id);
        return R.ok();
    }

}
